package com.books.activity;

import android.content.Intent;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.TextView;

import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.alibaba.android.arouter.utils.TextUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.SPStaticUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.books.R;
import com.books.adapter.BooksHistoryAdapter;

import com.books.databinding.BooksSearchRecordActivityBinding;
import com.google.android.flexbox.FlexDirection;
import com.google.android.flexbox.FlexWrap;
import com.google.android.flexbox.FlexboxLayoutManager;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;

import base.QActivity;
import books.room.BooksHistoryModel;
import books.room.BooksHistoryViewModel;

public  class BooksSearchRecordActivity extends QActivity {

    private BooksSearchRecordActivityBinding binding;
    private BooksHistoryAdapter booksHistoryAdapter;
    private BooksHistoryViewModel viewModel;
    private ArrayList<String> newList = new ArrayList<>();
    private LinkedHashMap<String, String> hashMap = new LinkedHashMap();
    @Override
    public int getLayoutRes() {
        return R.layout.books_search_record_activity;
    }

    @Override
    public void initView() {

        binding = DataBindingUtil.setContentView(this, getLayoutRes());

        viewModel = new ViewModelProvider(this).get(BooksHistoryViewModel.class);
    }

    @Override
    public void initData() {
        FlexboxLayoutManager manager = new FlexboxLayoutManager(this, FlexDirection.ROW, FlexWrap.WRAP) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        };
        binding.rvBooksHistory.setLayoutManager(manager);
        booksHistoryAdapter = new BooksHistoryAdapter(R.layout.books_history_item, null);
        binding.rvBooksHistory.setAdapter(booksHistoryAdapter);

    }

    @Override
    public void initListener() {
        viewModel.getAllSLive().observe(this, new Observer<List<BooksHistoryModel>>() {
            @Override
            public void onChanged(List<BooksHistoryModel> historyModels) {
                newList.clear();

                for (int i = 0; i < historyModels.size(); i++) {
                    hashMap.put(historyModels.get(i).getName(), historyModels.get(i).getName());
                }

                for (LinkedHashMap.Entry<String, String> entry : hashMap.entrySet()) {

                    newList.add(entry.getValue());
                }
                Collections.reverse(newList);

                booksHistoryAdapter.replaceData(newList);
            }
        });


        binding.edSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                //以下方法防止两次发送请求
                if (actionId == EditorInfo.IME_ACTION_SEND ||
                        (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
                    switch (event.getAction()) {
                        case KeyEvent.ACTION_UP:
                            getLRYSearch();
                            return true;
                        default:
                            return true;
                    }
                }
                return false;
            }

        });

        binding.ivSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                getLRYSearch();
            }
        });


        binding.ivDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewModel.deleteTableData();

                SPStaticUtils.put("BooksSearchId", 0);

                v.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        newList.clear();
                        booksHistoryAdapter.replaceData(newList);
                        ToastUtils.showLong("已清空搜索记录");
                    }
                }, 100);
            }
        });
    }

    @Override
    public void networkMessage() {

    }


    public void getLRYSearch() {
        String searchName = binding.edSearch.getText().toString().trim();

        if (TextUtils.isEmpty(searchName)) {
            ToastUtils.showLong("请输入搜索内容");
            return;
        }

        int searchId = SPStaticUtils.getInt("BooksSearchId", 0);
        int newsearchId = searchId + 1;
        LogUtils.d("保存的Id" + newsearchId);
        SPStaticUtils.put("BooksSearchId", newsearchId);
        viewModel.addHistoryDao(new BooksHistoryModel(newsearchId, searchName));

        viewModel.getAllSLive().getValue();


        Intent intent = new Intent(this, BooksSearchActivity.class);
        intent.putExtra("searchName", searchName);
        startActivity(intent);
    }
}
